package org.piggy.core.handler;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.piggy.common.config.ServiceDefinition;
import org.piggy.common.config.ServiceInstance;
import org.piggy.core.manager.DynamicConfigManager;
import org.piggy.register.center.api.RegisterCenterHandler;
import java.util.Set;

@Slf4j
public class NacosRegisterCenterHandler implements RegisterCenterHandler {
    @Override
    public void onChange(ServiceDefinition serviceDefinition, Set<ServiceInstance> serviceInstanceSet) {
        DynamicConfigManager manager = DynamicConfigManager.getInstance();
        manager.addServiceInstance(serviceDefinition.getServiceName(), serviceInstanceSet);
        manager.putServiceDefinition(serviceDefinition.getServiceName(), serviceDefinition);
        log.info("refresh service and instance: {} {}", serviceDefinition.getServiceName(), JSON.toJSON(serviceInstanceSet));
    }
}
